//
//  main.m
//  NSDate
//
//  Created by Snow on 16/7/11.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    /* NSDate
     
     计算当前时间和一个固定时间的差值,如果差值在60秒内,输出"刚刚",如果在60秒外3600秒内,输出"xx分钟前",如果3600秒外, 3600*24秒内,输出"xx小时前"
     将字符串@"2016年04月01日 01点20分18秒"转换为NSDate对象。
     将1468232420转成日期类型
     研究NSDateComponents */
    //1
    NSDate *currentDate = [NSDate date];
    NSDate *date1 = [[NSDate alloc] initWithTimeInterval:60 sinceDate:currentDate];
    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:3600 sinceDate:currentDate];
    NSDate *date3 = [[NSDate alloc] initWithTimeInterval:3600 * 24 sinceDate:currentDate];
    if ([currentDate laterDate:date1]) {
        NSLog(@"刚刚");
    } else if ([currentDate laterDate:date2]) {
        NSLog(@"XX分钟前");
    } else if ([currentDate laterDate:date3]) {
        NSLog(@"XX小时前");
    }
    //2
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY年MM月dd日 hh点mm分ss秒"];
    NSDate *time = [dateFormatter dateFromString:@"2016年04月01日 01点20分18秒"];
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate: time];
    
    NSDate *localDate = [time  dateByAddingTimeInterval: interval];
    
    NSLog(@"%@", localDate);
    //3
    NSDate *dateOfThree = [NSDate dateWithTimeIntervalSince1970:1468232420];
    NSInteger interval1 = [zone secondsFromGMTForDate: dateOfThree];
    NSDate *localDate1 = [dateOfThree dateByAddingTimeInterval:interval1];
    NSLog(@"%@", dateOfThree);
    NSLog(@"%@", localDate1);
    
    return 0;
}
